Test Series - Data Structure

Test Number 111/115

Q: Space complexity for an adjacency list of an undirected graph having large values of V (vertices) and E (edges) is ___________
A. O(E)
B. O(V*V)
C. O(E+V)
D. O(V)
Solution: In an adjacency list for every vertex there is a linked list which have the values of the edges to which it is connected.
Q: For some sparse graph an adjacency list is more space efficient against an adjacency matrix.
A. True
B. False
C. ...
D. ...
Solution: Space complexity for adjacency matrix is always O(V*V) while space complexity for adjacency list in this case would be O(V).
Q: Time complexity to find if there is an edge between 2 particular vertices is _________
A. O(V)
B. O(E)
C. O(1)
D. O(V+E)
Solution: The maximum edges a vertex can have is V-1.
Q: For the given conditions, which of the following is in the correct order of increasing space requirement?
i) Undirected, no weight
ii) Directed, no weight
iii) Directed, weighted
iv) Undirected, weighted
A. ii iii i iv
B. i iii ii iv
C. iv iii i ii
D. i ii iii iv
Solution: i) takes v+4e, ii) takes v+2e, iii) takes v+3e, iv) takes v +6e space.
Q: Space complexity for an adjacency list of an undirected graph having large values of V (vertices) and E (edges) is __________
A. O(V)
B. O(E*E)
C. O(E)
D. O(E+V)
Solution: In an adjacency list for every vertex there is a linked list which have the values of the edges to which it is connected.
Q: Complete the given snippet of code for the adjacency list representation of a weighted directed graph.

	class neighbor
        {
		int vertex, weight;
		____ next;
	}
 
	class vertex
        {
		string name;
		_____ adjlist;
	}
 
	vertex adjlists[101];
A. vertex, vertex
B. neighbor, vertex
C. neighbor, neighbor
D. vertex, neighbor
Solution: Vertex would have a name and a linked list attached to it.
Q: In which case adjacency list is preferred in front of an adjacency matrix?
A. Dense graph
B. Sparse graph
C. Adjacency list is always preferred
D. Complete graph
Solution: In case of sparse graph most of the entries in the adjacency matrix would be 0, hence adjacency list would be preferred.
Q: To create an adjacency list C++’s map container can be used.
A. True
B. False
C. ...
D. ...
Solution: We can create a mapping from string to a vector, where string would be the name of the vertex and vector would contains the name of the vertices to which it is connected.
Q: What would be the time complexity of the following function which adds an edge between two vertices i and j, with some weight ‘weigh’ to the graph having V vertices?

vector adjacent[15] ;
vector weight[15]; 
 
void addEdge(int i,int j,int weigh) 
{	 
	adjacent[a].push_back(i); 
	adjacent[b].push_back(j); 
	weight[a].push_back(weigh); 
	weight[b].push_back(weigh); 
}
A. O(1)
B. O(V)
C. O(V*V)
D. O(log V)
Solution: The function win in the constant time as all the four step takes constant time.
Q: What would be the time complexity of the BFS traversal of a graph with n vertices and n1.25 edges?
A. O(n)
B. O(n1.25)
C. O(n2.25)
D. O(n*n)
Solution: The time complexity for BFS is O(|V| + |E|) = O(n + n1.25) = O(n1.25).

You Have Score    /10